/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BusquedaPagos.java
 *
 * Created on 31/07/2011, 12:26:48 PM
 */
package co.srm.vistas.funcionales;

import co.srm.utilidades.IconCellRender;
import co.srm.dao.JPAGenericDAO;
import co.srm.entidades.Pago;
import co.srm.entidades.Prestamo;
import co.srm.manejadores.ManejadorPago;
import co.srm.manejadores.ManejadorPrestamo;
import co.srm.vistas.VentanaPrincipal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import org.jdesktop.swingx.JXTable.IconRenderer;
import org.jdesktop.swingx.decorator.Filter;
import org.jdesktop.swingx.decorator.FilterPipeline;
import org.jdesktop.swingx.decorator.PatternFilter;

/**
 *
 * @author TilZon
 */
public class BusquedaPagos extends javax.swing.JDialog {

    private List<Pago> pagoMemoria = null;
    private Pago elPago = null;
    private int laFila = 0;
    private int laColumna = 0;
    public static Integer nuevaCuota = 0;
    ManejadorPago mPago = null;
    ManejadorPrestamo mPrestamo = null;

    /** Creates new form BusquedaPagos */
    public BusquedaPagos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        inicializar();
    }

    public void inicializar() {
        this.setLocationRelativeTo(null);
        llenarComboConLosFiltros();
        mPago = new ManejadorPago();
        mPrestamo=new ManejadorPrestamo();
        editarPago.setVisible(false);
        nuevaCuota = 0;
        if (VentanaPago.codigoPres != null) {
            idPrestamo.setText(VentanaPago.codigoPres);
            SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
            String fe_actual = formato.format(new Date());
            llenarTablaPagos();
            Fecha.setText(fe_actual);
            FechaActionPerformed(null);

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        jXPanel3 = new org.jdesktop.swingx.JXPanel();
        jLabel13 = new javax.swing.JLabel();
        editarPago = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaPagos = new org.jdesktop.swingx.JXTable();
        jLabel1 = new javax.swing.JLabel();
        idPrestamo = new javax.swing.JTextField();
        listarPagos = new javax.swing.JButton();
        Fecha = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        saldoActual = new javax.swing.JTextField();
        totalRecaudo = new javax.swing.JTextField();
        neto = new javax.swing.JTextField();
        jXPanel2 = new org.jdesktop.swingx.JXPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        campoCliente = new javax.swing.JTextField();
        campoTelefono = new javax.swing.JTextField();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jInternalFrame1.setVisible(true);

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jXPanel3.setBackground(new java.awt.Color(255, 220, 98));
        jXPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));
        jXPanel3.setForeground(new java.awt.Color(255, 255, 255));

        jLabel13.setFont(new java.awt.Font("Arial", 3, 18));
        jLabel13.setText("Busqueda de Pagos");

        editarPago.setFont(new java.awt.Font("Tahoma", 1, 14));
        editarPago.setText("Editar");
        editarPago.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarPagoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel3Layout = new javax.swing.GroupLayout(jXPanel3);
        jXPanel3.setLayout(jXPanel3Layout);
        jXPanel3Layout.setHorizontalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel3Layout.createSequentialGroup()
                .addContainerGap(220, Short.MAX_VALUE)
                .addComponent(jLabel13)
                .addGap(173, 173, 173)
                .addComponent(editarPago)
                .addGap(27, 27, 27))
        );
        jXPanel3Layout.setVerticalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editarPago)
                    .addComponent(jLabel13))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tablaPagos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "Fecha", "Cuota", "Editar", "Eliminar"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaPagos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaPagosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablaPagos);
        tablaPagos.getColumnModel().getColumn(0).setResizable(false);
        tablaPagos.getColumnModel().getColumn(1).setResizable(false);
        tablaPagos.getColumnModel().getColumn(2).setResizable(false);
        tablaPagos.getColumnModel().getColumn(3).setResizable(false);
        tablaPagos.getColumnModel().getColumn(4).setResizable(false);
        tablaPagos.setDefaultRenderer(Object.class, new IconCellRender());

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel1.setText("CODIGO PRESTAMO:");

        idPrestamo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idPrestamoActionPerformed(evt);
            }
        });

        listarPagos.setText("Listar Pagos");
        listarPagos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listarPagosActionPerformed(evt);
            }
        });

        Fecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FechaActionPerformed(evt);
            }
        });
        Fecha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                FechaKeyReleased(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Fecha:");

        jXPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, java.awt.Color.lightGray, java.awt.Color.darkGray));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setText("Total Prestado: $");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Total Recaudo: $");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Saldo Actual: $");

        saldoActual.setEditable(false);

        totalRecaudo.setEditable(false);

        neto.setEditable(false);

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addComponent(saldoActual, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel1Layout.createSequentialGroup()
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(neto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                            .addComponent(totalRecaudo, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(neto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(totalRecaudo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(saldoActual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15))
        );

        jXPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, java.awt.Color.lightGray, java.awt.Color.darkGray));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setText("Telefono");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Cliente");

        campoCliente.setEditable(false);

        campoTelefono.setEditable(false);

        javax.swing.GroupLayout jXPanel2Layout = new javax.swing.GroupLayout(jXPanel2);
        jXPanel2.setLayout(jXPanel2Layout);
        jXPanel2Layout.setHorizontalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(25, 25, 25)
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(campoTelefono)
                    .addComponent(campoCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE))
                .addContainerGap())
        );
        jXPanel2Layout.setVerticalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(campoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                        .addComponent(jXPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(idPrestamo, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(listarPagos)
                        .addGap(41, 41, 41)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(Fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18))
            .addComponent(jXPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 639, Short.MAX_VALUE)
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addComponent(idPrestamo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(listarPagos))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(jLabel2))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(Fecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                    .addComponent(jXPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tablaPagosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaPagosMouseClicked
        if (evt.getClickCount() == 1) {
            // trae el punto donde di clik en la fila, me trae la celda
            int fila = tablaPagos.rowAtPoint(evt.getPoint());
            // en que columna estoy, los dos me traen en que celda estoy
            int columna = tablaPagos.columnAtPoint(evt.getPoint());
            if ((fila > -1) && (columna > -1)) {
                int columnaId = 0;
                laFila = fila;
                laColumna = 2;
                String id = tablaPagos.getValueAt(fila, columnaId).toString();
                Pago pago = buscarPago(id);
                elPago = pago;
                if (columna == 3) {
                    new EditarCuota(null, true).setVisible(true);
                } else {
                    if (columna == 4) {
                        eliminarPAgo(pago);
                    }
                }

            }

        } else {

            JOptionPane.showMessageDialog(null, "Por favor seleccione un pago");
        }


}//GEN-LAST:event_tablaPagosMouseClicked

    private void listarPagosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listarPagosActionPerformed
        llenarTablaPagos();
    }//GEN-LAST:event_listarPagosActionPerformed
    private void FechaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_FechaKeyReleased
        // TODO add your handling code here:
        hacerLosFiltros();
    }//GEN-LAST:event_FechaKeyReleased
    private void editarPagoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarPagoActionPerformed
        int r = JOptionPane.showConfirmDialog(this, "Esta seguro de editar este pago\nVerifique que es el que desea modificar.", "Actualizar Pago", JOptionPane.YES_NO_OPTION);

        if (r == JOptionPane.YES_OPTION) {
            Double nuevaCuotaL = Double.parseDouble(nuevaCuota.toString());
            String idInicial = tablaPagos.getValueAt(laFila, 0).toString();

            Prestamo p = elPago.getPrestamo();
            Double saldo = p.getSaldoActual();
            Double cuotaAnterior = elPago.getAbono();
            Double recaudoActual = VentanaPrincipal.totalSesion;

//            if (idInicial.equals(elPago.getId().toString())) {
            if ((nuevaCuotaL != cuotaAnterior) && (p.getNeto() >= nuevaCuotaL)) {

                saldo += cuotaAnterior;
                recaudoActual -=
                        cuotaAnterior;
                elPago.setAbono(nuevaCuotaL);
                saldo -= nuevaCuotaL;
                recaudoActual +=
                        nuevaCuotaL;
                p.setSaldoActual(saldo);

                VentanaPrincipal.totalSesion = recaudoActual;
                VentanaPrincipal.recaudoHoy.setText(recaudoActual.toString());

                if (p.getSaldoActual() > 0) {
                    p.setEstadoActual(Prestamo.EstadoActual.VIGENTE);
                } //                if (p.getSaldoActual() == 0) {
                //                    p.setEstadoActual(Prestamo.EstadoActual.SALDADO);
                //                }
                boolean actualizo = mPago.actualizarPago(elPago);

                if (actualizo) {
                    JOptionPane.showMessageDialog(null, "Prestamo Actualizado Correctamente");
                    limpiarTabla();
                    llenarTablaPagos();
                } else {
                    JOptionPane.showMessageDialog(null, "Prestamo no no se pudo actualizar");
                }
            } else {
                JOptionPane.showMessageDialog(null, "El valor es igual al inicial verifiquelo \nó La cuota es mayor a lo prestado", "Actualizar Pago", JOptionPane.INFORMATION_MESSAGE);
            } //            } else {
            //                JOptionPane.showMessageDialog(null, "Ha seleccionado un pago diferente al inicial", "Actualizar Pago", JOptionPane.INFORMATION_MESSAGE);
            //            }
        }
    }//GEN-LAST:event_editarPagoActionPerformed

    public void eliminarPAgo(Pago p) {
        int r = JOptionPane.showConfirmDialog(this, " Desea Realmente Eliminar este Pago ", "Advertencia", JOptionPane.YES_NO_OPTION);
         Double recaudoActual=0.0;
        if (r == JOptionPane.YES_OPTION) {
            Prestamo pre = p.getPrestamo();
            Double saldo = pre.getSaldoActual();
            Double cuotaAnterior = p.getAbono();
             recaudoActual = VentanaPrincipal.totalSesion;
            boolean elimino = mPago.eliminarPago(p.getId());
            if (elimino) {
                JOptionPane.showMessageDialog(null, "Pago eliminado correctamente", "Eliminar Pago", JOptionPane.INFORMATION_MESSAGE);
                saldo += cuotaAnterior;
                if(p.getFecha().equals(new Date()))
                {
                    recaudoActual -= cuotaAnterior;//si el pago pertenece al recaudo del dia de hoy

                VentanaPrincipal.totalSesion = recaudoActual;
                VentanaPrincipal.recaudoHoy.setText(recaudoActual.toString());
                }
                pre.setSaldoActual(saldo);
                List<Pago> pagos=pre.getPagos();
                pagos.remove(p);
                pre.setPagos(pagos);
                mPrestamo.actualizarPrestamo(pre);
                limpiarTabla();
                llenarTablaPagos();
                
                
            }
        } else {
            JOptionPane.showMessageDialog(null, "Pago no Eliminado", "Eliminar Prestamo", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private void idPrestamoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idPrestamoActionPerformed
        llenarTablaPagos();
    }//GEN-LAST:event_idPrestamoActionPerformed
    private void FechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FechaActionPerformed
        FechaKeyReleased(null);
    }//GEN-LAST:event_FechaActionPerformed
    public void llenarTablaPagos() {
        
        String idPrestamoT = idPrestamo.getText();
        saldoActual.setText("");
        totalRecaudo.setText("");


        if (!idPrestamoT.isEmpty()) {
            Prestamo prestamo = mPrestamo.consultarPrestamoByCodigo(idPrestamoT);


            if (prestamo != null) {

                List<Pago> pagos = prestamo.getPagos();
                pagoMemoria = pagos;
                DefaultTableModel dtm = (DefaultTableModel) tablaPagos.getModel();
                Double recaudo = 0.0;

                campoCliente.setText(prestamo.getCliente().getNombres() + " " + prestamo.getCliente().getApellidos());
                campoTelefono.setText(prestamo.getCliente().getTelefono() + " - " + prestamo.getCliente().getCelular());


                for (Pago p : pagos) {
                    ImageIcon iconEdi = new ImageIcon(getClass().getResource("/co/srm/vistas/iconos/modificar2.png"));
                    ImageIcon iconEli = new ImageIcon(getClass().getResource("/co/srm/vistas/iconos/Delete2.png"));
                    JLabel jeliminar = new JLabel(iconEli);
                    JLabel jeditar = new JLabel(iconEdi);

                    Object[] fila = {p.getId(), p.getFecha(), p.getAbono(), jeditar, jeliminar};
                    dtm.addRow(fila);
                    recaudo +=
                            p.getAbono();


                }
                saldoActual.setText(prestamo.getSaldoActual().toString());
                totalRecaudo.setText(recaudo.toString());
                neto.setText(prestamo.getNeto().toString());

                llenarComboConLosFiltros();


            } else {
                JOptionPane.showMessageDialog(null, "Prestamo no encontrado");


            }
        } else {
            JOptionPane.showMessageDialog(null, "Por favor ingrese un prestamo valido");


        }

    }

    public void llenarComboConLosFiltros() {

        //ocultando columna id star
        tablaPagos.getColumnModel().getColumn(0).setMaxWidth(0);
        tablaPagos.getColumnModel().getColumn(0).setMinWidth(0);
        tablaPagos.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
        tablaPagos.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
        //ocultando columna id end

        tablaPagos.getColumnModel().getColumn(3).setMaxWidth(95);
        tablaPagos.getColumnModel().getColumn(3).setMinWidth(95);
        tablaPagos.getTableHeader().getColumnModel().getColumn(3).setMaxWidth(0);
        tablaPagos.getTableHeader().getColumnModel().getColumn(3).setMinWidth(0);

        tablaPagos.getColumnModel().getColumn(4).setMaxWidth(95);
        tablaPagos.getColumnModel().getColumn(4).setMinWidth(95);
        tablaPagos.getTableHeader().getColumnModel().getColumn(4).setMaxWidth(95);
        tablaPagos.getTableHeader().getColumnModel().getColumn(4).setMinWidth(95);


    }

    private void hacerLosFiltros() {
        try {
            limpiarTabla();
            String filtro = Fecha.getText();


            if (!filtro.isEmpty() && filtro != null) {
                //errorLabel.setVisible(false);
                //columna-=1;
                //FilterPipeline filtros = new FilterPipeline(new Filter[] { new PatternFilter(filtro, 0, 0),new PatternFilter(filtro,1, 1) });
                PatternFilter p1 = new PatternFilter(filtro, -1, 1);
                Filter[] filterArray = {p1};
                FilterPipeline filters = new FilterPipeline(filterArray);

                tablaPagos.setFilters(filters);


            } else {
                Filter[] filterArray = {};
                FilterPipeline filters = new FilterPipeline(filterArray);
                tablaPagos.setFilters(filters);


                if (filtro == null || filtro.isEmpty()) // errorLabel.setVisible(false);
                {
                    System.out.println("Digite una fecha...");


                }

            }

            llenarTablaPagosEnMemoria();







        } catch (Exception ex) {
            Logger.getLogger(BusquedaCliente.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }


    }

    public void llenarTablaPagosEnMemoria() {
        DefaultTableModel dtm = (DefaultTableModel) tablaPagos.getModel();


        for (Pago p : pagoMemoria) {
            ImageIcon iconEdi = new ImageIcon(getClass().getResource("/co/srm/vistas/iconos/modificar2.png"));
            ImageIcon iconEli = new ImageIcon(getClass().getResource("/co/srm/vistas/iconos/Delete2.png"));
            JLabel jeliminar = new JLabel(iconEli);
            JLabel jeditar = new JLabel(iconEdi);

            Object[] fila = {p.getId(), p.getFecha(), p.getAbono(), jeditar, jeliminar};
            dtm.addRow(fila);


        }

    }

    public void limpiarTabla() {
        DefaultTableModel dtm = (DefaultTableModel) tablaPagos.getModel();
        dtm.setRowCount(0);


    }

    public Pago buscarPago(String id) {
        Long idCliente = null;
        Pago p = null;


        try {
            idCliente = Long.parseLong(id);


        } catch (Exception e) {
            return null;


        }
        for (Pago cliente : pagoMemoria) {
            if (cliente.getId().equals(idCliente)) {
                p = cliente;


                break;


            }
        }

        return p;


    }
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                BusquedaPagos dialog = new BusquedaPagos(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Fecha;
    private javax.swing.JTextField campoCliente;
    private javax.swing.JTextField campoTelefono;
    public static javax.swing.JButton editarPago;
    public static javax.swing.JTextField idPrestamo;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel2;
    private org.jdesktop.swingx.JXPanel jXPanel3;
    private javax.swing.JButton listarPagos;
    private javax.swing.JTextField neto;
    private javax.swing.JTextField saldoActual;
    private org.jdesktop.swingx.JXTable tablaPagos;
    private javax.swing.JTextField totalRecaudo;
    // End of variables declaration//GEN-END:variables
}
